import { fileURLToPath, URL } from "node:url";

import { defineConfig, splitVendorChunkPlugin, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    base: "./",
    build: {
      outDir: "reportforms",
      chunkSizeWarningLimit: 1000,
    },
    server: {
      open: "/",
      port: 11000,
      host: "0.0.0.0",
      proxy: {
        "/system": {
          target: env.VITE_APP_TARGET,
          changeOrigin: true,
        },
        "/report/forms": {
          target: env.VITE_APP_TARGET,
          rewrite: (path) => path.replace(/^\/report\/forms/, ""),
          changeOrigin: true,
        },
      },
    },
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    plugins: [
      vue(),
      vueJsx(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      splitVendorChunkPlugin(),
    ],
  };
});
